<template>
  <div class="page-header-index-wide">
    <a-row :gutter="24">

      <!-- 订单总数 -->
      <a-col :sm="24" :md="12" :xl="6" :style="{ marginBottom: '24px' }">
        <chart-card :loading="loading" title="完成订单总数" :total="this.orderNumAndSales.orderTotal | NumberFormat">
          <a-tooltip title="指标说明" slot="action">
            <a-icon type="info-circle-o" />
          </a-tooltip>
          <template slot="footer">平均销售额<span>￥ {{(this.orderNumAndSales.allMoney/this.orderNumAndSales.orderTotal*1.0).toFixed(2)}}</span></template>
        </chart-card>
      </a-col>

      <!-- 订单总销售额 -->
      <a-col :sm="24" :md="12" :xl="6" :style="{ marginBottom: '24px' }">
        <chart-card :loading="loading" title="订单总销售额" :total="this.orderNumAndSales.allMoney | NumberFormat">
          <a-tooltip title="指标说明" slot="action">
            <a-icon type="info-circle-o" />
          </a-tooltip>
          <template slot="footer">平均销售额<span>￥ {{(this.orderNumAndSales.allMoney/this.orderNumAndSales.orderTotal*1.0).toFixed(2)}}</span></template>
        </chart-card>
      </a-col>

      <!-- 订单状态 -->
      <a-col :sm="24" :md="12" :xl="6" :style="{ marginBottom: '24px' }">
        <chart-card :loading="loading" title="订单状态-订单数" >
          <a-tooltip title="指标说明" slot="action">
            <a-icon type="info-circle-o" />
          </a-tooltip>
          <div>
            <mini-bar :height="40" />
          </div>
          <template slot="footer">订单总数 <span>{{this.total}}</span></template>
        </chart-card>
      </a-col>

      <!-- 时间-销售额 -->
      <a-col :sm="24" :md="12" :xl="6" :style="{ marginBottom: '24px' }">
        <chart-card :loading="loading" title="时间-销售额" >
          <a-tooltip title="指标说明" slot="action">
            <a-icon type="info-circle-o" />
          </a-tooltip>
          <div>
            <mini-area />
          </div>
          <template slot="footer">销售总额<span> {{this.orderNumAndSales.allMoney}}</span></template>
        </chart-card>
      </a-col>
    </a-row>

    <a-card :loading="loading" :bordered="false" :body-style="{padding: '0'}">
      <div class="salesCard">
        <a-tabs default-active-key="1" size="large" :tab-bar-style="{marginBottom: '24px', paddingLeft: '16px'}">
          <a-tab-pane loading="true" tab="销售额" key="1">
            <a-row>
              <a-col :xl="16" :lg="12" :md="12" :sm="24" :xs="24">
                <div>
                  <v-chart :forceFit="true" :height="height" :data="data" :scale="scale">
                    <v-tooltip :showTitle="false" dataKey="item*percent" />
                    <v-axis />
                    <v-legend dataKey="item" />
                    <v-pie position="percent" color="item" :v-style="pieStyle" :label="labelConfig" />
                    <v-coord type="theta" />
                  </v-chart>
                </div>
              </a-col>
              <a-col :xl="8" :lg="12" :md="12" :sm="24" :xs="24">
                <rank-list title="门店销量前十排行榜" :list="rankList"/>
              </a-col>
            </a-row>
          </a-tab-pane>
        </a-tabs>
      </div>
    </a-card>

  </div>
</template>

<script>
  import ChartCard from '@/components/ChartCard'
  import ACol from "ant-design-vue/es/grid/Col"
  import ATooltip from "ant-design-vue/es/tooltip/Tooltip"
  import MiniArea from '@/components/chart/MiniArea'
  import MiniBar from '@/components/chart/MiniBar'
  import MiniProgress from '@/components/chart/MiniProgress'
  import RankList from '@/components/chart/RankList'
  import Bar from '@/components/chart/Bar'
  import LineChartMultid from '@/components/chart/LineChartMultid'
  import HeadInfo from '@/components/tools/HeadInfo.vue'
  import Trend from '@/components/Trend'
  import { getLoginfo,getVisitInfo } from '@/api/api'

  const rankList = []

  const barData = []
  for (let i = 0; i < 12; i += 1) {
    barData.push({
      x: `${i + 1}月`,
      y: Math.floor(Math.random() * 1000) + 200
    })
  }

  import { getAction,get } from '../../api/manage'
  export default {
    name: "IndexChart",
    components: {
      ATooltip,
      ACol,
      ChartCard,
      MiniArea,
      MiniBar,
      MiniProgress,
      RankList,
      Bar,
      Trend,
      LineChartMultid,
      HeadInfo
    },
    data() {
      return {
        loading: true,
        center: null,
        rankList,
        barData,
        loginfo:{},
        visitFields:['ip','visit'],
        visitInfo:[],
        indicator: <a-icon type="loading" style="font-size: 24px" spin />,
        
        sourceData: [],
        data:[],
        scale:[{
          dataKey: 'percent',
          min: 0,
          formatter: '.0%',
        }],
        height: 400,
        pieStyle: {
          stroke: "#fff",
          lineWidth: 1
        },
        labelConfig: ['percent', {
          formatter: (val, item) => {
            return item.point.item + ': ' + val;
          }
        }],
        orderNumAndSales:{},
        sourceDataTemp:[],
        total: 0,
      }
    },
    created() {
      setTimeout(() => {
        this.loading = !this.loading
      }, 1000)
      this.initLogInfo();
    },
    methods: {
      initLogInfo () {
        getLoginfo(null).then((res)=>{
          if(res.success){
            Object.keys(res.result).forEach(key=>{
              res.result[key] =res.result[key]+""
            })
            this.loginfo = res.result;
          }
        })
        getVisitInfo().then(res=>{
          if(res.success){
             this.visitInfo = res.result;
           }
         })
      },

      // 查询订单总数和总金额
      getOrderTotallAndSalesTotal(){
        get('/schoolOrder/web/selectOrderChart').then((res) => {
          // JSON.parse(JSON.stringify(res.result));
          var temp = res.data;
          this.orderNumAndSales = temp;
        })
      },

      // 按月销降序排序
      getAllShop() {
        get('/schoolOrder/web/selectShopInfoOrderBySellCount').then((res) => {
          console.log(res.data);
          // JSON.parse(JSON.stringify(res.result));
          var temp = res.data;
          var DataSet = require('@antv/data-set');
          for (let i = 0; i < 10; i++) {
            this.sourceData.push({
              item: temp[i].name,
              count: temp[i].sellCount,
            });
            rankList.push({
              name: temp[i].name,
              total: temp[i].sellCount,
            }); 
          }
          var dv = new DataSet.View().source(this.sourceData);
          dv.transform({
            type: 'percent',
            field: 'count',
            dimension: 'item',
            as: 'percent'
          });
          this.data = dv.rows;
        })
      },

      getAllorderAndSetStatus() {
        get('/schoolOrder/web/selectAllOrder').then((res) => {
          // JSON.parse(JSON.stringify(res.result));
          var orderList = res.data;
          for(var i=0; i<orderList.length; i++){
            this.total++;
          }
        })
      },
    },
    mounted() {
      this.getOrderTotallAndSalesTotal();
      this.getAllShop();
      this.getAllorderAndSetStatus();
    },
  }
</script>

<style lang="less" scoped>
  .circle-cust{
    position: relative;
    top: 28px;
    left: -100%;
  }
  .extra-wrapper {
    line-height: 55px;
    padding-right: 24px;

    .extra-item {
      display: inline-block;
      margin-right: 24px;

      a {
        margin-left: 24px;
      }
    } 
  }

  /* 首页访问量统计 */
  .head-info {
    position: relative;
    text-align: left;
    padding: 0 32px 0 0;
    min-width: 125px;

    &.center {
      text-align: center;
      padding: 0 32px;
    }

    span {
      color: rgba(0, 0, 0, .45);
      display: inline-block;
      font-size: .95rem;
      line-height: 42px;
      margin-bottom: 4px;
    }
    p {
      line-height: 42px;
      margin: 0;
      a {
        font-weight: 600;
        font-size: 1rem;
      }
    }
  }
</style>